﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Objects.DataClasses;
using System.ComponentModel;

namespace CCF20Model
{
    public partial class VehicleMaintenanceRecord : IDataErrorInfo, IEntityWithContext
    {
        private CCF20Entities _context;
        public CCF20Entities Context 
        {
            get
            {
                return _context;
            }
            set
            {
                _context = value;
            }

        }
        public VehicleMaintenanceRecord()
        {
            
        }

        partial void OnMaintenanceTypeIDChanged()
        {
            if (this.EntityState != EntityState.Detached)
            {
                this.RemoveError("MaintenanceTypeID");
                if (_context == null)
                {
                    //only when get the record from db,you have the _context
                    //But if you get the record from web service for new device. there is no context.
                    return;
                }
                int count = _context.VehicleMaintenanceRecords.Count(
                    m=>m.MaintenanceTypeID == this.MaintenanceTypeID &&
                    m.VehicleMainTenanceRecordID != this.VehicleMainTenanceRecordID);
                            
                if (count > 0)
                {
                    this.AddError("MaintenanceTypeID", "重复的保养项目");
                }
            }
        }

        #region IDataErrorInfo Members

        private Dictionary<string, string> m_validationErrors = new Dictionary<string, string>();

        private void AddError(string columnName, string msg)
        {
            if (!m_validationErrors.ContainsKey(columnName))
            {
                m_validationErrors.Add(columnName, msg);
            }

        }

        private void RemoveError(string columnName)
        {
            if (m_validationErrors.ContainsKey(columnName))
            {
                m_validationErrors.Remove(columnName);
            }
        }

        public string Error
        {
            get
            {
                if (m_validationErrors.Count > 0)
                {
                    return "Customer data is invalid";
                }
                else return null;
            }
        }

        public string this[string columnName]
        {
            get
            {
                if (m_validationErrors.ContainsKey(columnName))
                {
                    return m_validationErrors[columnName];
                }
                else return null;
            }
        }

        #endregion
    }
}
